/**********************************************
*  Namespace: AI
*  Classes:   SteeringPursue
*  Desc:      Updates positions using a Pursue
*             algorithm in 2d or 3d cases
*  Author:    Vaios Kalpias-Ilias (C) 2008-2009
***********************************************/

#include "SteeringPursue.h"
#include "SteeringSeek.h"

namespace AI
{
	//---------------------------------------------
	/**
	* Ctor
	*/
	SteeringPursue::SteeringPursue()
	{
	}
	//---------------------------------------------
	/**
	* Dtor
	*/
	SteeringPursue::~SteeringPursue()
	{
	}
	//---------------------------------------------
	/**
	* Returns new steering data in 2d
	*/
	Steering2d SteeringPursue::GetSteering(const Kinematic2d& current,
										   const Kinematic2d& target)
	{
		// get the direction to the target
		mth::Vector2 direction(target.position - current.position);
		float distance = direction.Length();
		
		// find our current speed
		float speed = current.velocity.Length();

		// check if speed is too small to give a reasonable
		// prediction time
		float prediction;
		if (speed <= distance / maxPrediction)
		{
			prediction = maxPrediction;
		}
		else
		{
			prediction = distance / speed;
		}

		// create our new target
		Kinematic2d predictTarget(target);
		predictTarget.position += target.velocity * prediction;

		// delegate to seek
		return SteeringSeek::GetSteering(current, predictTarget);


	}

}
